//
//  MessageCell.swift
//  QQ
//
//  Created by zhang on 16/1/7.
//  Copyright © 2016年 jin. All rights reserved.
//

import UIKit

class MessageCell: UITableViewCell {
    
    var messageFrame:MessageFrame!
        {
        didSet
        {
            self.assignSubViewsContent()
            self.assignSubViewsFrame()
        }
    }
    var iconView:UIImageView!
    var timeView:UILabel!
    var textView:UIButton!
    //便捷构造器
    class func instanceWithTableView(tableView:UITableView)->MessageCell
    {
        let identifier = "qqcell"
        var cell = tableView.dequeueReusableCellWithIdentifier(identifier)
        if cell == nil
        {
            cell = MessageCell(style: UITableViewCellStyle.Default, reuseIdentifier: identifier)
        }
        let temp = cell as! MessageCell
        return temp
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        //设置头像
        self.iconView = UIImageView()
        self.iconView.layer.cornerRadius = 25
        self.iconView.layer.masksToBounds = true
        //设置时间
        self.timeView = UILabel()
        self.timeView.font = UIFont.systemFontOfSize(timeFontSize)
        self.timeView.textAlignment = NSTextAlignment.Center
        //设置内容
        self.textView = UIButton()
        self.textView.titleLabel?.numberOfLines = 0
        self.textView.titleLabel?.font = UIFont.systemFontOfSize(textFontSize)
        self.textView.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        self.textView.titleEdgeInsets = UIEdgeInsets(top: buttonEdgeInsets, left: buttonEdgeInsets, bottom: buttonEdgeInsets, right: buttonEdgeInsets)
        //添加进父控件
        self.addSubview(self.iconView)
        self.addSubview(self.timeView)
        self.addSubview(self.textView)
        
        //点击收回键盘
    }
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        super.touchesBegan(touches, withEvent: event)
        self.superview?.superview?.superview?.endEditing(true)
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //给子控件赋值
    func assignSubViewsContent()
    {
        self.iconView.image = UIImage(imageLiteral: self.messageFrame.message.icon)
        self.timeView.text = self.messageFrame.message.time as String
        self.textView.setTitle(self.messageFrame.message.text as String, forState: UIControlState.Normal)
        //好友和自己发送的图片是不一样的，这例做下处理
        var normalImageName = ""
        var higlightedImageName = ""
        if self.messageFrame.message.type == 0
        {
            normalImageName = "chat_send_nor"
            higlightedImageName = "chat_send_press_pic"
        }
        else
        {
            normalImageName = "chat_recive_nor"
            higlightedImageName = "chat_recive_press_pic"
        }
        //实例化图片对象并调用扩展的方法的到需要的图片
        let normalImage = UIImage(imageLiteral: normalImageName).changeImage()
        let highlightedImage = UIImage(imageLiteral: higlightedImageName).changeImage()
        //设置不同状态的背景图片
        self.textView.setBackgroundImage(normalImage, forState: UIControlState.Normal)
        self.textView.setBackgroundImage(highlightedImage, forState: UIControlState.Highlighted)
        //清除当前cell背景颜色
        self.backgroundColor = UIColor.clearColor()
        //设置消息主题内容
        self.textView.setTitle(self.messageFrame.message.text as String, forState: UIControlState.Highlighted)
        
    }
    //设置位置
    func assignSubViewsFrame()
    {
        self.iconView.frame = self.messageFrame.iconFrame
        self.timeView.frame = self.messageFrame.timeFrame
        self.textView.frame = self.messageFrame.textFrame
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
